package com.lxp.ssh2.web.action.users;

import java.util.Date;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.dispatcher.SessionMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.lxp.ssh2.po.User;
import com.lxp.ssh2.service.UserService;
import com.lxp.ssh2.web.Constents;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Namespace("/")
public class SessionAction extends ActionSupport {

	@Autowired
	private UserService userService;

	private String account;
	private String password;
	private String username;
	private String repassword;

	public void setAccount(String account) {
		this.account = account;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	@Action(value = "signin", results = { @Result(name = SUCCESS, location = "home/signin.jsp") })
	public String signin() {
		return SUCCESS;
	}

	/**
	 * 登录认证
	 * 
	 * 
	 * home/index.action struts.xml中配置了result它就会跳进去
	 */

	@Action(value = "authen", results = {
			@Result(name = SUCCESS, type = "redirect", location = "home/index.action"),
			@Result(name = ERROR, location = "home/signin.jsp") })
	public String authenticate() {
		if (account == null || password == null || "".equals(account.trim())
				|| "".equals(password.trim())) {
			Map<String, String> request = (Map<String, String>) ActionContext
					.getContext().get("request");
			request.put("nullUser", "用户名或密码不能为空");
			return ERROR;
		}
		User user = userService.findByAccount(account);
		if (user == null || !password.equals(user.getPassword())) {
			Map<String, String> request = (Map<String, String>) ActionContext
					.getContext().get("request");
			request.put("errorUser", "用户名或密码错误");
			return ERROR;
		}
		ActionContext.getContext().getSession()
				.put(Constents.CURRENT_USER, user.getName());
		return SUCCESS;

	}

	/**
	 * 退出登录
	 */
	@Action(value = "signout", results = { @Result(name = SUCCESS, type = "redirectAction", location = "signin") })
	public String signout() {
		// 使session失效
		SessionMap<String, Object> session = (SessionMap<String, Object>) ActionContext
				.getContext().getSession();
		session.invalidate();
		// 第二种方式
		// ServletActionContext.getRequest().getSession().invalidate();
		return SUCCESS;
	}

	
	/**
	 * 验证用户注册
	 */
	@Action(value="register", results={
			@Result(name=SUCCESS, location="home/signin.jsp")
	})
	public String register() {
		if(account == null || password == null || username == null || repassword == null || "".equals(account.trim())
				|| "".equals(password.trim()) || "".equals(username.trim()) || "".equals(repassword.trim())) {
			Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
			request.put("isNull", "注册提醒：不能留空");
			return SUCCESS;
		}
		if(!password.equals(repassword)) {
			Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
			request.put("isError", "注册提醒：两次密码不同");
			return SUCCESS;
		}
		User user = new User();
		user.setAccount(account);
		user.setName(username);
		user.setPassword(password);
		user.setCreatedAt(new Date());
		user.setUpdatedAt(new Date());
		userService.addUser(user);
		if(userService.findByAccount(account) != null) {
			Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
			request.put("isTrue", "注册提醒：你已注册成功");
			return SUCCESS;
		}
		return SUCCESS;
	}
}
